from app.extensions import db
from sqlalchemy.orm import deferred
from datetime import datetime
from werkzeug.security import check_password_hash

<<<<<<< HEAD
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
class ModuleType:
    # 管理员
    ADMIN_USER = '用户管理'
    ADMIN_LOG = '日志管理'
    USER_PROFILE = '用户信息'
    ADMIN_ROLE = '权限管理'
<<<<<<< HEAD
    
=======


>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
class OperationType:
    # 操作类型
    ADD = '新增'
    UPDATE = '修改'
    DELETE = '删除'
    QUERY = '查询'
    OTHER = '其他'
    RESET_PASSWORD = '重置密码'
    CHANGE_PASSWORD = '修改密码'
<<<<<<< HEAD
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), unique=True, nullable=False, index=True)
    password = deferred(db.Column(db.String(255), nullable=False))
    is_active = db.Column(db.Boolean, nullable=False, default=True)
<<<<<<< HEAD
    create_time = db.Column(db.DateTime, nullable=False, index=True, default=datetime.now)
    
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'), nullable=False, index=True)
    
=======
    create_time = db.Column(db.DateTime, nullable=False,
                            index=True, default=datetime.now)

    role_id = db.Column(db.Integer, db.ForeignKey(
        'role.id'), nullable=False, index=True)

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    role = db.relationship('Role', backref='users', uselist=False)
    profile = db.relationship('UserProfile', backref='user', uselist=False)

    def check_password(self, password):
        return check_password_hash(self.password, password)
<<<<<<< HEAD
    
    def to_json(self):
        name = self.profile.name if self.profile else ''
        # role 有 admin,user 两种
        role =  self.role.name if self.role.code else ''
        create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            'id':self.id,
            'name':name,
            'email':self.email,
            'role':role,
            'is_active':self.is_active,
            'create_time':create_time,
        }   

class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)  
    code = db.Column(db.String(50), unique=True, nullable=False)  
    description = db.Column(db.String(200))  
    create_time = db.Column(db.DateTime, default=datetime.now)
    
    permissions = db.relationship('Permission', 
                                secondary='role_permission',
                                backref=db.backref('roles', lazy='dynamic'),
                                lazy='dynamic')
    
    def to_json(self):
        return {
            'id': self.id,
            'name': self.name,
            'code': self.code,
            'description': self.description,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        }

class Permission(db.Model):
    __tablename__ = 'permission'
    id = db.Column(db.Integer, primary_key=True)
    module = db.Column(db.String(50), nullable=False, index=True)  # 对应 ModuleType
    operation = db.Column(db.String(30), nullable=False, index=True)  # 对应 OperationType
    description = db.Column(db.String(200))  # 权限描述
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)
    
=======

    def to_json(self):
        name = self.profile.name if self.profile else ''
        # role 有 admin,user 两种
        role = self.role.name if self.role.code else ''
        create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            'id': self.id,
            'name': name,
            'email': self.email,
            'role': role,
            'is_active': self.is_active,
            'create_time': create_time,
        }


class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)
    code = db.Column(db.String(50), unique=True, nullable=False)
    description = db.Column(db.String(200))
    create_time = db.Column(db.DateTime, default=datetime.now)

    permissions = db.relationship('Permission',
                                  secondary='role_permission',
                                  backref=db.backref('roles', lazy='dynamic'),
                                  lazy='dynamic')

    def to_json(self):
        return {
            'id': self.id,
            'name': self.name,
            'code': self.code,
            'description': self.description,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        }


class Permission(db.Model):
    __tablename__ = 'permission'
    id = db.Column(db.Integer, primary_key=True)
    module = db.Column(db.String(50), nullable=False,
                       index=True)  # 对应 ModuleType
    operation = db.Column(db.String(30), nullable=False,
                          index=True)  # 对应 OperationType
    description = db.Column(db.String(200))  # 权限描述
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    def to_json(self):
        return {
            'id': self.id,
            'module': self.module,
            'operation': self.operation,
            'description': self.description,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        }

<<<<<<< HEAD
class RolePermission(db.Model):
    __tablename__ = 'role_permission'
    id = db.Column(db.Integer, primary_key=True)
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'), nullable=False, index=True)
    permission_id = db.Column(db.Integer, db.ForeignKey('permission.id'), nullable=False, index=True)
=======

class RolePermission(db.Model):
    __tablename__ = 'role_permission'
    id = db.Column(db.Integer, primary_key=True)
    role_id = db.Column(db.Integer, db.ForeignKey(
        'role.id'), nullable=False, index=True)
    permission_id = db.Column(db.Integer, db.ForeignKey(
        'permission.id'), nullable=False, index=True)
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)


class UserProfile(db.Model):
    __tablename__ = 'user_profile'
    id = db.Column(db.Integer, primary_key=True)
<<<<<<< HEAD
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, index=True)
=======
    user_id = db.Column(db.Integer, db.ForeignKey(
        'user.id'), nullable=False, index=True)
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    name = db.Column(db.String(80), nullable=True, index=True)
    phone = db.Column(db.String(20), nullable=True, index=True)
    gender = db.Column(db.String(10), nullable=True, index=True)
    birthday = db.Column(db.Date, nullable=True, index=True)
    address = db.Column(db.Text, nullable=True)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)
<<<<<<< HEAD
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
    
    def to_json(self):
        email = self.user.email if self.user else ''
        birthday = self.birthday.strftime('%Y-%m-%d') if self.birthday else ''
        update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else ''
        return {
            'id':self.id,
            'email':email,
            'name':self.name,
            'phone':self.phone,
            'gender':self.gender,
            'birthday':birthday,
            'address':self.address,
            'update_time':update_time,
        }

=======
    update_time = db.Column(db.DateTime, nullable=False,
                            default=datetime.now, onupdate=datetime.now)

    def to_json(self):
        email = self.user.email if self.user else ''
        birthday = self.birthday.strftime('%Y-%m-%d') if self.birthday else ''
        update_time = self.update_time.strftime(
            '%Y-%m-%d %H:%M:%S') if self.update_time else ''
        return {
            'id': self.id,
            'email': email,
            'name': self.name,
            'phone': self.phone,
            'gender': self.gender,
            'birthday': birthday,
            'address': self.address,
            'update_time': update_time,
        }


>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
class LoginLog(db.Model):
    __tablename__ = 'login_log'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), nullable=False, index=True)
    ip = db.Column(db.String(40), nullable=False, index=True)
    user_agent = db.Column(db.String(200), nullable=True)
    status = db.Column(db.Boolean, nullable=False, default=True)
    fail_reason = db.Column(db.String(200), nullable=True)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)

    def parse_user_agent(self):
        if not self.user_agent:
            return ''
        user_agent = self.user_agent.lower()
        if 'micromessenger' in user_agent:
            return '微信浏览器'
        elif 'edge' in user_agent:
            return 'Edge浏览器'
        elif 'firefox' in user_agent:
            return 'Firefox浏览器'
        elif 'chrome' in user_agent:
            return 'Chrome浏览器'
        elif 'safari' in user_agent and 'chrome' not in user_agent:
            return 'Safari浏览器'
        elif 'opera' in user_agent or 'opr' in user_agent:
            return 'Opera浏览器'
        elif 'msie' in user_agent or 'trident' in user_agent:
            return 'IE浏览器'
        else:
            return '其他浏览器'
<<<<<<< HEAD
        
        
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    def to_json(self):
        return {
            'id': self.id,
            'email': self.email,
            'ip': self.ip,
            'user_agent': self.parse_user_agent(),
            'status': '成功' if self.status else '失败',
            'fail_reason': self.fail_reason,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        }

<<<<<<< HEAD
=======

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
class OperationLog(db.Model):
    __tablename__ = 'operation_log'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, index=True)
    email = db.Column(db.String(120), nullable=False, index=True)
<<<<<<< HEAD
    module = db.Column(db.String(50), nullable=False, index=True) 
    operation = db.Column(db.String(30), nullable=False, index=True)  
    detail = db.Column(db.Text, nullable=True)  
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)
    
    def to_json(self):
        return {
            'id': self.id,
            'email':self.email,
=======
    module = db.Column(db.String(50), nullable=False, index=True)
    operation = db.Column(db.String(30), nullable=False, index=True)
    detail = db.Column(db.Text, nullable=True)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)

    def to_json(self):
        return {
            'id': self.id,
            'email': self.email,
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
            'module': self.module,
            'operation': self.operation,
            'detail': self.detail,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
<<<<<<< HEAD
        }    
=======
        }
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
